Skip to content

Update the longrope inv_freq buffer in place - #50480

Merged
hmellor merged 1 commit into
huggingface:mainfrom
hmellor:longrope-pure-update
Nov 9, 2026
Merged

hmellor merged 1 commit into
huggingface:mainfrom
hmellor:longrope-pure-update

Conversation

@hmellor

@hmellor hmellor commented Nov 8, 2026 •

Copy link
Copy Markdown
Member

🤗 Transformers CI

What does this PR do?

Follow-up to #49555. longrope_frequency_update now picks the long or short frequencies with torch.where, but it still re-registers inv_freq on every forward, replacing the buffer with a new tensor. Under CUDA graphs the captured kernels keep reading the original buffer, so the switch never reaches them; vLLM rejects the module for this reason. This PR copies into the existing buffer instead, so it keeps its address.

Evidence

test_longrope_updates_inv_freq_in_place checks the buffer keeps its identity while switching long → short; it fails on main. test_longrope_compiled_matches_eager (from #49555) still passes.

End to end, microsoft/Phi-3.5-mini-instruct through vLLM's Transformers modeling backend (B200, bf16, max_model_len=8192, piecewise CUDA graphs), compared with vLLM's native Phi3:

Transformers Result
main Assigning / modifying buffers of nn.Module during forward pass is not allowed when using cudagraph
This PR 7206-token prompt (long factors): identical greedy tokens, max logprob diff 0.16

The vLLM side also needs vllm-project/vllm#65446, which narrows that check to real _buffers writes.

AI assistance (Claude Code) was used to write this PR; I reviewed and validated it.

Before submitting

  • This PR fixes a typo or improves the docs (you can dismiss the other checks if that's the case).
  • Did you read the contributor guideline and the
    Pull Request checks?
  • Was this discussed/approved via a Github issue or the forum? Please add a link
    to it if that's the case.
  • Did you make sure to update the documentation with your changes according to the guidelines?
  • Did you write any new necessary tests?

Who can review?

@vasqu @zucchini-nlp (reviewed #49555)

`longrope_frequency_update` re-registered `inv_freq` on every forward,
replacing the buffer with a new tensor. CUDA graphs replay from the
buffer's original address, so the replaced tensor is never read and
vLLM rejects the module under CUDA graphs. Copy into the existing buffer
instead.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions

github-actions Bot commented Nov 8, 2026

Copy link
Copy Markdown
Contributor

[For maintainers] Suggested jobs to run (before merge)

run-slow: phi3

@HuggingFaceDocBuilderDev

Copy link
Copy Markdown

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

@zucchini-nlp zucchini-nlp left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks reasonable to me, thx!

@hmellor

hmellor commented Nov 9, 2026

Copy link
Copy Markdown
Member Author

run-slow: phi3

@hmellor
hmellor marked this pull request as ready for review November 9, 2026 10:29
@hmellor
hmellor added this pull request to the merge queue Nov 9, 2026
Merged via the queue into huggingface:main with commit b255688 Nov 9, 2026
129 of 130 checks passed
@hmellor
hmellor deleted the longrope-pure-update branch November 9, 2026 11:21
inv_freq = torch.where(seq_len > original_max_position_embeddings, long_inv_freq, original_inv_freq)
self.register_buffer(f"{prefix}inv_freq", inv_freq, persistent=False)
# Update in place so the buffer keeps its address, which CUDA graphs replay from
getattr(self, f"{prefix}inv_freq").copy_(inv_freq)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wondering whether we need something similar for dynamic freq updates?

@zucchini-nlp zucchini-nlp Nov 9, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it is still not compile friendly overall (input-dependent seq_len > max_len, jiqing tried but it led to computational overhead at each forward step

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was motivated by vLLM, but ultimately I now must choose either long or short depending on how vLLM is configured. This is how vLLM treats log rope for its native models too

@github-actions

github-actions Bot commented Nov 9, 2026

Copy link
Copy Markdown
Contributor

CI (Nvidia + AMD)

Nvidia · ✅ No new failures 🎉👏 · ⚙️ Workflow · 📊 Dashboard

Failures (1 pre-existing)

    Pre-existing failures (still failing) — 1 total:

        Same error (1):

        - phi3:
            tests/models/phi3/test_modeling_phi3.py::Phi3IntegrationTest::test_export_static_cache

AMD · ✅ No new failures 🎉👏 · ⚙️ Workflow · 📊 Dashboard

Failures (5 pre-existing)

    Pre-existing failures (still failing) — 5 total:

        Same error (5):

        - phi3:
            tests/models/phi3/test_modeling_phi3.py::Phi3IntegrationTest::test_export_static_cache
            tests/models/phi3/test_modeling_phi3.py::Phi3IntegrationTest::test_phi3_mini_128k_instruct_with_static_cache
            tests/models/phi3/test_modeling_phi3.py::Phi3IntegrationTest::test_phi3_mini_4k_instruct_generation
            tests/models/phi3/test_modeling_phi3.py::Phi3IntegrationTest::test_phi3_mini_4k_instruct_with_static_cache
            tests/models/phi3/test_modeling_phi3.py::Phi3IntegrationTest::test_phi3_mini_4k_sliding_window

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants